﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    /* Classes implementam regras alem de salvar dados.
     * Struct apenas usado para salvar dados.
     */
    /// <summary>
    /// 
    /// </summary>
    class AnimatedObject:Object2D
    {
        List<Animation> myAnimation;
        public Animation currentAnimation;
         Animation CurrentAnimation
        {
            get { return currentAnimation; }
            set { currentAnimation = value; }
        }

        public AnimatedObject(List<Animation> myAnimation,string startAnimationName,
            Texture2D i, bool c) :
            base(i, c)
        {
            this.myAnimation = myAnimation;
            currentAnimation = GetAnimation(startAnimationName);
            this.Rect = new Rectangle((int)currentAnimation.startPosition.X,
                (int)currentAnimation.startPosition.Y,
                currentAnimation.frameSize.Width,
                currentAnimation.frameSize.Height);
        }

        public Animation GetAnimation(string Name)
        {
            return myAnimation.Find(delegate(Animation a) { return a.Name == Name; });
        }

        public void SetAnimation(string Name)
        {
            currentAnimation = myAnimation.Find(delegate(Animation a) { return a.Name == Name; });
        }

        public override void Update(GameTime pGameTime)
        {
            if (currentAnimation.lastTime + currentAnimation.frameTime
                > pGameTime.TotalGameTime.TotalMilliseconds)
            {
                return;
            }

            currentAnimation.lastTime = (float)pGameTime.TotalGameTime.TotalMilliseconds;
            currentAnimation.currentFrame.X++;
            if (currentAnimation.currentFrame.X > currentAnimation.totalFrames.X)
            {
                currentAnimation.currentFrame.X = 0;
                currentAnimation.currentFrame.Y++;
                if (currentAnimation.currentFrame.Y > currentAnimation.totalFrames.Y)
                {
                    currentAnimation.currentFrame.Y = 0;
                }

            }
            this.Rect = new Rectangle(
                (int)(currentAnimation.startPosition.X + 
                currentAnimation.frameSize.Width*
                currentAnimation.currentFrame.X),
                (int)(currentAnimation.startPosition.Y +
                currentAnimation.frameSize.Height *
                currentAnimation.currentFrame.Y),
                currentAnimation.frameSize.Width,
                currentAnimation.frameSize.Height);


        }

    }


    /// <summary>
    /// 
    /// </summary>
    struct Animation
    {
        public Vector2 totalFrames;
        public Vector2 currentFrame;
        public Rectangle frameSize;
        public float frameTime;
        public float lastTime;

        public Vector2 startPosition;
        public string Name;

        public Animation(string Name, Vector2 totalFrames,Vector2 startPosition,
            Rectangle frameSize, float frameTime)
        {
            this.Name = Name;
            this.totalFrames = totalFrames;
            this.startPosition = startPosition;
            this.frameSize = frameSize;
            this.frameTime = frameTime;
            currentFrame = Vector2.Zero;
            lastTime = 0;
        }

        public Animation(Animation clone)
        {
            this.Name = clone.Name;
            this.totalFrames = clone.totalFrames;
            this.startPosition = clone.startPosition;
            this.frameSize = clone.frameSize;
            this.frameTime = clone.frameTime;
            currentFrame = clone.currentFrame;
            lastTime = clone.lastTime;
        }
    }
}
